#include "dialog_game.h"
#include "ui_dialog_game.h"

Dialog_Game::Dialog_Game(QWidget *parent, LayerProcess* _pProcess) :
	QDialog(parent),
	ui(new Ui::Dialog_Game)
{
	ui->setupUi(this);
	this->setEnabled(false);
	
	pProcess = _pProcess;
	pProcess->game_Init(ui->progressBar, ui->progressBar_2);
	pProcess->game_randomizeWords();
	
	ui->lineEdit->clear();
	ui->lineEdit->setEnabled(true);
	ui->label_4->setEnabled(true);
	ui->label_3->clear();
	ui->label_5->setVisible(false);
	ui->label_curWord->setText(pProcess->game_GetWord());
	isGettingResults = e_SM_Game_GetResults;
	
	this->setEnabled(true);
	ui->lineEdit->setFocus();
}


Dialog_Game::~Dialog_Game()
{
	pProcess->game_close();
	delete ui;
	emit finished(0);
}


void Dialog_Game::closeEvent(QCloseEvent *)
{	
	this->~Dialog_Game();
}


void Dialog_Game::on_pushButton_clicked()
{
    this->close();
}


void Dialog_Game::on_pushButton_Validate_clicked()
{
	this->setEnabled(false);
	
	switch(isGettingResults)
	{
	case e_SM_Game_InitWord:
		
		
		/* Recupere un nouveau mot l'affichage */
		ui->label_curWord->setText(pProcess->game_GetWord());
		
		ui->lineEdit->clear();
		ui->label_3->clear();
		ui->label_5->setVisible(false);
		this->setEnabled(true);
		ui->label_4->setEnabled(true);
		ui->lineEdit->setEnabled(true);
		ui->lineEdit->setFocus();
		
		 /* Transition d'etat */
		isGettingResults = e_SM_Game_GetResults;
		
		
		break;
	case e_SM_Game_GetResults:
		
		
		ui->lineEdit->setDisabled(true);
		ui->label_4->setDisabled(true);
		ui->label_5->setVisible(true);
		
		if( pProcess->game_GetResult(ui->lineEdit->text()) == true )
		{
			/* son OK */
			pProcess->PlaySound(e_Sounds_Ok);
			QImage pic(":/Pics/Pics/Ok_Voca_small.png");
			if(!pic.isNull())
				ui->label_5->setPixmap(QPixmap::fromImage(pic));
			ui->label_3->setText("Correct!");
		}
		else
		{
			QImage pic(":/Pics/Pics/No_Voca_small.png");
			if(!pic.isNull())
				ui->label_5->setPixmap(QPixmap::fromImage(pic));
			ui->label_3->setText("The correct answer was: \n" + pProcess->game_GetWordResult() );
		}
		
		this->setEnabled(true);
		
		/* Transition d'etat */
		if(ui->progressBar->value() < ui->progressBar->maximum())
		{
			isGettingResults = e_SM_Game_InitWord;
		}
		else
		{
			isGettingResults = e_SM_Game_Congrats;
		}
		
		
		break;
	case e_SM_Game_Congrats:
		
		
		this->setEnabled(true);
		/* GAME FINISHED */
		pProcess->PlaySound(e_Sounds_End);
		
		ui->label_3->setText("Congratulations!\nYour score is: " + QString::number( ui->progressBar_2->value() ) + "%");
		
		/* Transition d'etat */
		isGettingResults = e_SM_Game_EndGame;
		
		
		break;
	case e_SM_Game_EndGame:
		
		
		this->close();
		
		
		break;
	default:
		break;
	}
	
	
	
}



